package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


@RestController
@RequestMapping("/users")
public class UserInfoController  {



    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService redisService;


    @GetMapping("/checkPhone")
    private Object checkPhone(String phone){
       Boolean ret= userInfoService.checkPhone(phone);
       return !ret;
    }

    @GetMapping("/sendVerifyCode")
    private Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    private Object regist(String phone, String nickname, String password , String rpassword, String verifyCode){

            userInfoService.regist( phone, nickname, password ,  rpassword, verifyCode);

        return JsonResult.success();
    }



    @PostMapping("/login")
    private Object login(String username ,String password ){

        UserInfo userInfo=userInfoService.login(username,password);
        //获取Tokne
        String token = userInfoRedisService.setToken(userInfo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user", userInfo);
        return JsonResult.success(map);
    }

    @GetMapping("/currentUser")
    private Object currentUser(HttpServletRequest request){

        //获取请求头的token
        String token = request.getHeader("token");
        //查询redis获取用户对象
      UserInfo userInfo= userInfoRedisService.getUserInfoToken(token);
        return  JsonResult.success(userInfo);
    }

    @GetMapping("/users")
    private Object users(HttpServletRequest request){
        //获取请求头的token
        String token = request.getHeader("token");
        //查询redis获取用户对象
        UserInfo userInfo= userInfoRedisService.getUserInfoToken(token);
        return  JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    private Object strategiesFavor(String userId){


        return  JsonResult.success(  redisService.getStrategiesFavor(userId));
    }

}
